﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;

namespace AChance.Tool.Func
{
    public static class AppFunAutoBuilderEnt
    {
        public const string VARCHAR = "A";
        public const string DECIMAL = "B";
        public const string INT = "C";
        public const string CHAR = "D";
        public const string TEXT = "E";
        public const string IMAGE = "F";
        public const string ACH_ENUM = "G";
        public const string ACH_DATE = "O";
        public const string ACH_TIME = "P";
        public const string ACH_REF = "X";
        private static DataTable _DtField;
        public static void AutoBuilder(string strTblName, string strTblDesc, DataTable dt, DataTable dtAll, string strProname, string _strWorkPath)
        {
            _DtField = dtAll;
            string strNullFore = "    ";
            string strpath = _strWorkPath + "\\" + strTblName.Trim() + ".cs";
            FileStream FS = new FileStream(strpath, FileMode.Create);        //新建目录下*.cs 
            StreamWriter sw = new StreamWriter(FS, Encoding.Default);
            if (dt.Rows.Count > 0)
            {
                #region 公共头
                sw.WriteLine("using System;");
                sw.WriteLine("using System.ComponentModel.DataAnnotations;");
                sw.WriteLine("using AChance.Dal.Func;");
                sw.WriteLine("");
                sw.WriteLine("namespace AChance.Dal.Entity");
                sw.WriteLine("{");
                sw.WriteLine(strNullFore + "[Serializable]");
                strTblDesc = strTblDesc.Trim();
                if (strTblDesc.EndsWith("表"))
                    strTblDesc = strTblDesc.Substring(0, strTblDesc.Length - 1);
                sw.WriteLine(strNullFore + "/// <summary>");
                sw.WriteLine(strNullFore + "/// " + strTblDesc);
                sw.WriteLine(strNullFore + "/// </summary>");
                sw.WriteLine(strNullFore + "[Display(Name = \"" + strTblDesc + "\")]");
                sw.WriteLine(strNullFore + "public partial class " + strTblName.Trim());

                sw.WriteLine(strNullFore + "{");
                #endregion

                string smallname;
                string strtype;
                string type;

                #region Members
                foreach (DataRow dcmbs in dt.Rows)
                {
                    DataRow dr = null;
                    try
                    {
                        dr = RefSource(dcmbs);
                    }
                    catch
                    {
                        //throw ex;
                    }
                    var bigname = dcmbs["字段"].ToString();
                    smallname = "_" + dcmbs["字段"].ToString().Substring(0, 1).ToLower() + dcmbs["字段"].ToString().Substring(1, dcmbs["字段"].ToString().Length - 1);
                    strtype = dr["类型"].ToString().Trim();
                    if (strtype == DECIMAL)
                        type = "double?";
                    else if (strtype == INT)
                        type = "int?";
                    else if (strtype == IMAGE)
                        type = "byte[]";
                    else if (strtype == ACH_ENUM)
                        type = $"DalState.{bigname.Trim()}?";
                    else
                        type = "string";
                    sw.WriteLine(strNullFore + strNullFore + "/// <summary>");
                    sw.WriteLine(strNullFore + strNullFore + "/// " + dcmbs["说明"].ToString());
                    sw.WriteLine(strNullFore + strNullFore + "/// </summary>");
                    sw.WriteLine(strNullFore + strNullFore + "[Display(Name = \"" + dcmbs["说明"].ToString() + "\")]");
                    sw.WriteLine(strNullFore + strNullFore + "public " + type.Trim().ToString() + " " + bigname.Trim().ToString() + " { get; set; }");


                }
                #endregion

                sw.WriteLine(strNullFore + "}");
                sw.WriteLine("}");
            }
            sw.Close();
            FS.Close();
        }
        private static DataRow RefSource(DataRow dr)
        {
            if (dr["类型"].ToString() != ACH_REF)
                return dr;
            string TbName = "";
            string FieName = "";
            try
            {
                TbName = dr["参照"].ToString().Substring(0, dr["参照"].ToString().IndexOf('.'));
                FieName = dr["参照"].ToString().Substring(dr["参照"].ToString().IndexOf('.') + 1);
            }
            catch
            {
                throw new Exception("参照字段格式错误");
            }
            DataRow[] RefFie = _DtField.Select("表名='" + TbName + "' AND 字段='" + FieName + "'");
            if (RefFie.Length == 0)
                throw new Exception("不存在" + dr["字段"].ToString() + "的字段");
            return RefSource(RefFie[0]);
        }
    }
}
